﻿Public Class FormAssemblingRecord
    Private WithEvents UcDigitalKeyboard1 As ucDigitalKeyboard
    Dim objFlbMst As New clsFlbMst
    Dim objFlbInfo As New clsFlbInfo
    Private Sub FormAssemblingRecord_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        setDgvData()
    End Sub
    Public Sub setDgvData()
        Dim dt As DataTable = objFlbInfo.getFlbInfoListByStatus("0")
        dt.Columns.Add("which_side_name")
        If dt.Rows.Count > 0 Then
            For Each row As DataRow In dt.Rows
                If row("which_side") = "0" Then
                    row("which_side_name") = "朝上"
                Else
                    row("which_side_name") = "朝下"
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub

    Private Sub btnTo_Click(sender As Object, e As EventArgs) Handles btnTo.Click
        If dgv.SelectedRows.Count > 0 Then
            txtDLNo.Text = dgv.SelectedRows(0).Cells("dl_no").Value.ToString()
            txtDLNo.Tag = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            Dim dtFlbMst As DataTable = objFlbMst.selectFlbByVal(dgv.SelectedRows(0).Cells("flb_type").Value.ToString())
            If dgv.SelectedRows(0).Cells("which_side").Value.ToString().Equals("0") Then
                SpinEdit2.Value = dtFlbMst.Rows(0)("count_up")
            Else
                SpinEdit2.Value = dtFlbMst.Rows(0)("count_down")
            End If
            SpinEdit3.Value = dgv.SelectedRows(0).Cells("piece_num").Value.ToString()
            SpinEdit3.Tag = dgv.SelectedRows(0).Cells("flb_type").Value.ToString()
        End If
    End Sub

    Private Sub btnFrom_Click(sender As Object, e As EventArgs) Handles btnFrom.Click
        If dgv.SelectedRows.Count > 0 Then
            TextEdit1.Text = dgv.SelectedRows(0).Cells("dl_no").Value.ToString()
            TextEdit1.Tag = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            Dim dtFlbMst As DataTable = objFlbMst.selectFlbByVal(dgv.SelectedRows(0).Cells("flb_type").Value.ToString())
            If dgv.SelectedRows(0).Cells("which_side").Value.ToString().Equals("0") Then
                SpinEdit1.Value = dtFlbMst.Rows(0)("count_up")
            Else
                SpinEdit1.Value = dtFlbMst.Rows(0)("count_down")
            End If
            SpinEdit4.Value = dgv.SelectedRows(0).Cells("piece_num").Value.ToString()
            SpinEdit4.Tag = dgv.SelectedRows(0).Cells("flb_type").Value.ToString()
        End If
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If String.IsNullOrEmpty(SpinEdit4.Tag.ToString()) = False And String.IsNullOrEmpty(SpinEdit3.Tag.ToString()) = False Then
            If SpinEdit4.Tag.ToString().Equals(SpinEdit3.Tag.ToString()) Then
                If txt_num.Value > 0 Then
                    If txt_num.Value <= SpinEdit4.Value Then
                        If txt_num.Value + SpinEdit3.Value <= SpinEdit2.Value Then
                            Dim rtn As Integer = objFlbInfo.save(txt_num.Value.ToString(), txtDLNo.Tag.ToString(), TextEdit1.Tag.ToString())
                            If rtn = -2 Then
                                MsgBox("保存失败"， vbYes, "提示")
                            Else
                                MsgBox("保存成功", vbYes, "提示")
                                setDgvData()
                                clear()
                            End If
                        Else
                            MsgBox("被合并框的被合并数量 + 目标框的已装配数量 需 <= 可装配数量")
                        End If
                    Else
                        MsgBox("被合并数量 需 <= 被合并框的已装配数量")
                    End If
                Else
                    MsgBox("被合并数量 需 >= 0")
                End If
            Else
                MsgBox("目标合并框规格型号与被合并框规格型号不一致！")
            End If
        Else
            MsgBox("请选择目标合并框和被合并框")
        End If
    End Sub

    Private Sub clear()
        txtDLNo.Text = String.Empty
        txtDLNo.Tag = String.Empty
        SpinEdit2.Value = 0
        SpinEdit3.Value = 0
        SpinEdit3.Tag = String.Empty
        TextEdit1.Text = String.Empty
        TextEdit1.Tag = String.Empty
        SpinEdit1.Value = 0
        SpinEdit4.Value = 0
        SpinEdit4.Tag = String.Empty
        txt_num.Value = 0
    End Sub

    Private Sub txt_num_MouseDown(sender As Object, e As MouseEventArgs) Handles txt_num.MouseDown
        UcDigitalKeyboard1 = New ucDigitalKeyboard
        If sender.MaskBox.Mask.EditMask = "d" Then
            UcDigitalKeyboard1.B_01 = False
        Else
            UcDigitalKeyboard1.B_01 = True
        End If

        If sender.GetType().Name.Equals("TextEdit") Then
            UcDigitalKeyboard1.B_02 = True
        Else
            UcDigitalKeyboard1.B_02 = False
        End If

        UcDigitalKeyboard1.Txt = sender

        Me.Controls.Add(UcDigitalKeyboard1)
        UcDigitalKeyboard1.Show()
        UcDigitalKeyboard1.BringToFront()
        UcDigitalKeyboard1.Focus()
    End Sub

End Class